#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
主要路由：健康检查、根路径等
"""

from flask import Blueprint, jsonify
from core import extensions

# 创建蓝图
main_bp = Blueprint('main', __name__)


@main_bp.route('/check', methods=['GET'])
def health_check():
    """
    健康检查接口
    返回标准格式的响应
    """
    response_data = {
        "code": 0,
        "message": "success",
        "data": None
    }
    return jsonify(response_data)


@main_bp.route('/', methods=['GET'])
def index():
    """
    根路径接口
    提供服务基本信息
    """
    response_data = {
        "code": 0,
        "message": "AI Class Elysium 服务运行中",
        "data": {
            "version": "1.0.0",
            "port": 18080,
            "endpoints": [
                "/check", 
                "/", 
                "/upload_file", 
                "/download_file", 
                "/multipart_upload", 
                "/upload_and_asr", 
                "/process_command", 
                "/batch_commands", 
                "/command_history",
                "/quick_command",
                "/supported_commands",
                "/voice_control/status", 
                "/voice_control/start", 
                "/voice_control/stop", 
                "/voice_control/devices", 
                "/voice_control/test_audio", 
                "/amap/status", 
                "/amap/tools",
                "/amap/search", 
                "/amap/weather", 
                "/amap/route", 
                "/amap/geocode",
                "/amap/regeo",
                "/amap/call_tool",
                "/text_to_image/generate",
                "/text_to_image/status"
            ]
        }
    }
    return jsonify(response_data)


@main_bp.route('/status', methods=['GET'])
def get_status():
    """获取系统状态"""
    try:
        status_data = {
            'backend_status': 'running',
            'voice_control_status': extensions.voice_control_service.get_status() if extensions.voice_control_service else None,
            'connected_clients': len(extensions.connected_clients),
            'amap_enabled': extensions.amap_service is not None,
            'amap_service_type': extensions.amap_service_type,
            'text_to_image_enabled': extensions.text_to_image_service is not None
        }
        return jsonify({
            "code": 0,
            "message": "success",
            "data": status_data
        })
    except Exception as e:
        return jsonify({
            "code": -1,
            "message": f"获取状态失败: {str(e)}",
            "data": None
        })


@main_bp.errorhandler(404)
def not_found(error):
    """404错误处理"""
    response_data = {
        "code": 404,
        "message": "接口不存在",
        "data": {"error": str(error)}
    }
    return jsonify(response_data), 404


@main_bp.errorhandler(500)
def internal_error(error):
    """500错误处理"""
    response_data = {
        "code": 500,
        "message": "服务器内部错误",
        "data": {"error": str(error)}
    }
    return jsonify(response_data), 500

